<template>
  <el-menu
    active-text-color="rgb(64, 158, 255)"
    background-color="#304156"
    class="el-menu-vertical-demo"
    :class="{ 'el-menu-collapse': isCollapse }"
    :default-active="defUrl"
    text-color="#fff"
    mode="vertical"
    router
    unique-opened
    :collapse="isCollapse"
    @open="handleOpen"
    @close="handleClose"
  >
    <!-- el-sub-menu  el-menu-item  el-menu-item-group-->
    <el-menu-item index="/index">
      <i class="iconfont homepage"></i>
      <span>首页</span>
    </el-menu-item>
    <el-sub-menu :index="'/' + p.name" v-for="p in menus" ::key="p.id">
      <template #title>
        <i :class="['iconfont ' + `${p.icon}`]"></i>
        <span>{{ p.title }}</span>
      </template>
      <template v-for="sub in p.children" :key="sub.id">
        <el-menu-item
          :index="'/' + p.name + '/' + sub.name"
          v-if="sub.hidden === 0"
        >
          <i :class="['iconfont ' + `${sub.icon}`]"></i>
          <span>{{ sub.title }}</span>
        </el-menu-item>
      </template>
    </el-sub-menu>
  </el-menu>
</template>

<script setup lang="ts">
import { useUserStore } from "@/store/user";
import { inject, ref, watch } from "vue";
import { useRoute } from "vue-router";
const userStore = useUserStore();
const isCollapse = inject<boolean>("isCollapse");
const menus = userStore.routes;
const defUrl = ref("/index");
const route = useRoute();

watch(
  () => route.path,
  () => {
    defUrl.value = route.path;
  },
  {
    immediate: true,
  }
);
const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
};
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
};
</script>

<style scoped lang="less">
:deep(.el-menu) {
  border: 0;
}
// .el-menu-vertical-demo:not(.el-menu--collapse) {
//   width: 250px;
// }
</style>
